package com.iSnail.ms.gather.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.iSnail.ms.util.Config;
import com.iSnail.ms.util.Const;

public class DBManager {
	private static final Logger logger = Logger.getLogger(DBManager.class.getName());
	private DBManager(){}
	
	static String driver = null;
	static String url = null;
	static String username = null;
	static String pwd = null;
	
	static{
		driver = Config.getConfigValue(Const.DB_DRIVER);
		url = Config.getConfigValue(Const.DB_URL);
		username = Config.getConfigValue(Const.DB_USERNAME);
		pwd = Config.getConfigValue(Const.DB_PWD);
	}
	
	public static Connection getCon(){
		Connection con=null;
        try {
        	con = getDataSourceCon();
        	if(con == null){
        		con = getDirectcon();
        	}
        } catch (Exception e) {
        	logger.error("",e);
        }
        return con;
	}
	
    private static Connection getDataSourceCon(){
        Connection con=null;
        try {
        	con = ConnPool.getConenction();
        } catch (Exception e) {
        	logger.error("",e);
        }
        return con;
    }
	
	
    private static Connection getDirectcon(){
        Connection con=null;
        try {
            Class.forName(driver);
            con=DriverManager.getConnection(url, username, pwd);
            logger.error("get DB Direct Connection!");
        } catch (Exception e) {
        	logger.error("",e);
        }
        return con;
    }

    public static Statement getSM(Connection con){
        Statement sm=null;
        if(con==null){
            return null;
        }else{
            try {
                sm=con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
            } catch (SQLException e) {
            	logger.error("",e);
            }
        }
        return sm;
    }

    public static PreparedStatement getPSM(Connection con,String sql){
        PreparedStatement sm=null;
        if(con==null){
            return null;
        }else{
            try {
                sm=con.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
            } catch (SQLException e) {
            	logger.error("",e);
            }
        }
        return sm;
    }

    public static ResultSet getRS(Statement sm,String sql){
        ResultSet rs=null;
        if(sm==null){
            return null;
        }else{
            try {
                rs=sm.executeQuery(sql);
            } catch (SQLException e) {
            	logger.error("",e);
            }
        }
        return rs;
    }

    public final static void closeDbSource(ResultSet rs,Statement sm,Connection con){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
            	logger.error("",e);
            }
        }
        if(sm!=null){
            try {
                sm.close();
            } catch (SQLException e) {
            	logger.error("",e);
            }
        }
        if(con!=null){
            try {
                con.close();
            } catch (SQLException e) {
            	logger.error("",e);
            }
        }
    }	
}